﻿using GalaSoft.MvvmLight;

namespace Budgetsch.WP7.ViewModel.Public
{
    using System.Collections.ObjectModel;
    using System.Windows;
    using System.Windows.Input;

    using Budgetsch.WP7.Model.DataService;
    using Budgetsch.WP7.Navigation;

    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class PublicViewModel : ViewModelBase
    {
        private readonly IPublicDataService _publicDataService;

        private readonly INavigationService _navigationService;

        /// <summary>
        /// The <see cref="IsBusy" /> property's name.
        /// </summary>
        public const string IsBusyPropertyName = "IsBusy";

        private bool _isBusy = false;

        /// <summary>
        /// Sets and gets the IsBusy property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public bool IsBusy
        {
            get
            {
                return _isBusy;
            }

            set
            {
                if (_isBusy == value)
                {
                    return;
                }

                _isBusy = value;
                RaisePropertyChanged(IsBusyPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="PublicUserViewModels" /> property's name.
        /// </summary>
        public const string PublicUserViewModelsPropertyName = "PublicUserViewModels";

        private ObservableCollection<PublicUserViewModel> _publicUserViewModels = new ObservableCollection<PublicUserViewModel>();

        /// <summary>
        /// Sets and gets the PublicUserViewModels property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<PublicUserViewModel> PublicUserViewModels
        {
            get
            {
                return _publicUserViewModels;
            }

            set
            {
                if (_publicUserViewModels == value)
                {
                    return;
                }

                _publicUserViewModels = value;
                RaisePropertyChanged(PublicUserViewModelsPropertyName);
            }
        }

        public ICommand SelectionChangedCommand { get; set; }

        /// <summary>
        /// Initializes a new instance of the PublicViewModel class.
        /// </summary>
        public PublicViewModel(IPublicDataService publicDataService, INavigationService navigationService)
        {
            _publicDataService = publicDataService;
            _navigationService = navigationService;

            SelectionChangedCommand = new RelayCommand<PublicUserViewModel>(p =>
            {
                if (p != null)
                {
                    p.NavigateToDetailsView();
                }
            });

        }

        public void GetPublicUsers()
        {
            IsBusy = true;
            _publicDataService.GetPublicUsers((users, e) =>
            {
                IsBusy = false;
                if (e != null)
                {
                    MessageBox.Show("Can't load the public users!", "Error", MessageBoxButton.OK);
                }
                else
                {
                    PublicUserViewModels.Clear();
                    foreach (var user in users)
                    {
                        PublicUserViewModels.Add(new PublicUserViewModel(user, _navigationService));
                    }
                }
            });
        }
    }
}